<%@ page language="java" import="java.util.*" pageEncoding="utf-8"%>
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
<%@ taglib prefix="fn" uri="http://java.sun.com/jsp/jstl/functions" %>
<%
String path = request.getContextPath();
String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
Object user = request.getSession().getAttribute("user");
if(user==null){
	response.sendRedirect(basePath+"login");
}
%>


<!DOCTYPE html>
<html>
  
  <head>
    <meta charset="UTF-8">
    <title>${sysconf.proname}</title>
    <meta name="renderer" content="webkit">
    <meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1">
    <link rel="stylesheet" href="css/xadmin.css">
    <script type="text/javascript" src="js/jquery1.min.js"></script>
    <script type="text/javascript" src="js/layui/layui.js" charset="utf-8"></script>
    <script type="text/javascript" src="js/xadmin.js"></script>
	<style type="text/css">
		.layui-input-block{margin: 0px}
		.hide{display: none}
	</style>
  </head>
  
 <body>
     <div class="x-body">
			<div class="layui-form topSear">
			  <div class="layui-form-item">
			  	<div class="layui-input-inline">
				    <a class="layui-btn layui-btn-normal" onclick="edit({})">添加</a>
				  </div>
			  </div>
			</div>
			
			<div class="topBox">
				
			</div>
			 <table class="layui-table" id="tabList"  lay-data="{height:'450',url:'statement/findLimitAcc?id=${id}',parseData:function(data){return parseData(data)},page:true, id:'tabList'}"  lay-filter="tabList">
			<!-- <table class="layui-table"  id="tabList" lay-data="{height:'550', url: 'json/list-1.json',parseData:function(data){return parseData(data)},page:true, id:'tabList'}" lay-filter="tabList"> -->
				<thead>
			    <tr>
			   	  <th lay-data="{field:'AID', width:80}">账号</th>
			      <th lay-data="{field:'ACCOUNT', width:150}">账号</th>
			      <th lay-data="{field:'USERNAME', width:80}">用户名</th>
			      <th lay-data="{field:'LIMITS'}">权限</th>
			      <th lay-data="{fixed: 'right', width:150,templet: '#caozuo'}">操作</th>
			    </tr>
			  </thead>
			</table>
			<script type="text/html" id="caozuo">
  				<a class="layui-btn layui-btn-primary layui-btn-xs" lay-event="edit"><i class="layui-icon">&#xe642;</i>编辑<a>
  				<a class="layui-btn layui-btn-primary layui-btn-xs" lay-event="del"><i class="layui-icon"> &#x1006</i>删除</a>
			</script>
		</div>

		<div class="edit" style="display: none">
			<form class="layui-form" style="margin: 1px" lay-filter="accountEdit_temp">
				<table class="layui-table insertTable">
				  <tbody>
				  	<tr>
				  	  <input type="hidden" name="id" value="" >
				      <td align="right" width="105"><span style="color: #f00;">*</span>账号ID：</td>
				      <td>
				      	<div class="layui-input-inline">
				      		<input type="text" name="aid"   data-id="accountSelect" value="" class="layui-input">
				      	</div>
				      </td>
				    </tr>
				    
				   <tr>
				      <td align="right">权限：</td>
				      <td colspan="3">
				      	<div class="layui-input-block" id="limit_list">
     						 <!-- <input lay-filter="limit" value="UP" type="checkbox" name="limits[UP]" title="修改" >
      						 <input lay-filter="limit" value="IN" type="checkbox" name="limits[IN]" title="添加">
      						 <input lay-filter="limit" value="IM" type="checkbox" name="limits[IM]" title="导入">
      						 <input lay-filter="limit" value="EXP" type="checkbox" name="limits[EXP]" title="导出"> -->
						</div>
				      </td>
				    </tr>
				    
				    <tr id="constraint_html" style="display: none">
				      <td align="right">约束：</td>
				      <td colspan="3">
				      	<div class="layui-input-block list" >
     						 <!-- <input lay-filter="limit" value="UP" type="checkbox" name="limits[UP]" title="修改" >
      						 <input lay-filter="limit" value="IN" type="checkbox" name="limits[IN]" title="添加">
      						 <input lay-filter="limit" value="IM" type="checkbox" name="limits[IM]" title="导入">
      						 <input lay-filter="limit" value="EXP" type="checkbox" name="limits[EXP]" title="导出"> -->
						</div>
				      </td>
				    </tr>
				    <a class="submit" lay-submit lay-filter="saveLimit" ></a>
				    

				  </tbody>
				</table>
			</form>
		</div>
		
		
<script type="text/javascript" src="js/layui_exts/exts.js"></script>
<script type="text/javascript" src="js/datautil.js"></script>
<script type="text/javascript" src="js/formdata.js"></script>
<script>

//返回的数据解析格式
function parseData(data){
	return {
      "code": data.code, //解析接口状态
      "msg": data.msg, //解析提示文本
      "count": data.count, //解析数据长度
      "data": data.list //解析数据列表
    };
}

var acc_limits = {};

layui.use(['table','laydate','tableSelect','form', 'layer','element'], function(){
  table = layui.table; 
  form = layui.form;
  tableSelect = layui.tableSelect;
  var laydate = layui.laydate;
  var laypage = layui.laypage,layer = layui.layer;	
  
   form.on('checkbox(limits)', function(data){
	  console.log(data.elem); //得到checkbox原始DOM对象
	  console.log(data.elem.checked); //是否被选中，true或者false
	  console.log(data.value); //复选框value值，也可以通过data.elem.value得到
	  console.log(data.othis); //得到美化后的DOM对象
	  var ischecked = data.elem.checked
	  var $input = $(data.elem);
	  var value = data.value;
	  var id = $input.attr("data-id");

	  var limitstr = acc_limits[id];
	  if(ischecked){
		  if(limitstr==null){
			  acc_limits[id] = 'SE|'+ value;
		  }else{
			  acc_limits[id] = limitstr+'|'+ value;
		  }
	  }else{
		  if(limitstr!=null){
			  acc_limits[id] = limitstr.replace('\|'+value,'');
		  }
	  }
	  
	  console.log(JSON.stringify(acc_limits));
	});  
  
  table.on('tool(tabList)',function(res){
	var data = res.data;
	switch (res.event) {
	
	case 'edit':
		edit(data);
		break;
	case 'del':
		del(data.ID)
		break;

	default:
		break;
	}
  })
  

  
})

//显示编辑框
function showEditDiv(layero,data){
	
	 //判断当前报表可配置哪些权限
    var limit_list = '<input lay-filter="limit" value="EXP" type="checkbox" name="limits[EXP]" title="导出">';
    if(data.inserts!=null&&data.inserts.length>2){
    	limit_list += '<input lay-filter="limit" value="IN" type="checkbox" name="limits[IN]" title="添加">';
		/*  <input lay-filter="limit" value="IM" type="checkbox" name="limits[IM]" title="导入"> */
    }
    if(data.updates!=null&&data.updates.length>2){
    	limit_list += '<input lay-filter="limit" value="UP" type="checkbox" name="limits[UP]" title="修改" >';
    }
    
    $(layero).find("#limit_list").html(limit_list);
    
    if(data.constraint_select_conf!=null&&data.constraint_select_conf.length>3){
    	var constraint_html = "";
    	var json = {};
    	try{
	    	json  = JSON.parse(data.constraint_select_conf);
    	}catch (e) {
			console.log("解析失败："+e);
		}
    	
    	constraint_html = FormData.toHtml(json, "");
    	console.log(constraint_html)
    	$(layero).find("#constraint_html .list").html(constraint_html);
    	$(layero).find("#constraint_html").show();
    }
	
	
	var id = data.id;  
  	if(id==null){
  		//选择人员
  		tableSelect.render({
			elem: '#accountSelect',	//定义输入框input对象
			checkedKey: 'ID',
			searchKey: 'username',	//搜索输入框的name值 默认keyword
			searchPlaceholder: '关键词搜索',	//搜索输入框的提示文字 默认关键词搜索
			table: {	//定义表格参数，与LAYUI的TABLE模块一致，只是无需再定义表格elem
				url:'statement/findAllAcc?sid=${param.id}',//'collection/findMyLegality',
				parseData:function(data){return parseData(data)},
				width:600,
				cols: [[
					{ type: id==null?'checkbox':'radio' },
					{ field: 'ID', title: 'ID', width:80 },
					{ field: 'DEPARTMENT', title: '部门', width:100 },
					{ field: 'ACCOUNT', title: '账号' },
					{ field: 'USERNAME', title: '用户名'}
				]]
			},
			done: function (elem, data) {
				var d = data.data;
				var dd = "";
				for(var i in d){
					dd+=','+d[i].ID
				}
				dd = dd.length>0?dd.substring(1):dd;
				var lll = {};
				console.log(JSON.stringify(dd))
				$("#accountSelect").val(dd);
			}
		})
  	}else{
	   
	    //数据回显
		var limits = data.limits;
		if(limits!=null){
			var ss = limits.split('\|');
			for(var i in ss){
				var ll = ss[i];
				data['limits['+ll+']']=true;
			}
		}
		
		var constraint_select =data.constraint_select;
		if(constraint_select!=null){
			var cs ={};
			try{
				cs = JSON.parse(data.constraint_select);
			}catch (e) {}
			
			for(var k in cs){
				data[k] = cs[k];
			}
		}
		
		
		$("#accountSelect").attr("ts-selected",data.aid);
		form.val("accountEdit",data);
		$(layero).find("input[name=aid]").attr("readonly","readonly");
  	}
	form.render();
}



/**编辑*/
function edit(data){
	var html = $(".edit").html().replace('accountEdit_temp','accountEdit').replace('data-id','id');
	layer.open({
      type: 1,
      title: "授权",
      shadeClose: true,		      
      maxmin: true, //开启最大化最小化按钮
      area: ['700px', '500px'],
      content: html,
      btn: ['保存', '取消'],
	  success: function(layero, index){
		  	acc_limits = {};
		    data = toLowerCase(data);
	  		$.post("statement/findStatementById",{"id":'${param.id}'},function(d){
	  			if(d.code==0){
	  				var dd = d.data;
	  				data.inserts = dd.INSERTS;
	  				data.updates = dd.UPDATES;
	  				data.updates = dd.UPDATES;
	  				data.constraint_select_conf = dd.CONSTRAINT_SELECT_CONF;
	  				showEditDiv(layero,data);
	  			}
	  			
	  		},"JSON")
	  },
	  yes: function(index, layero){
		  $(layero).find(".submit").click();
		  form.on('submit(saveLimit)',function(data){
			  var field  = data.field;
			  var param = {'limits':'SE','sid':${id}};
			  var constraint = {};
			  for(var k in field){
				  var v = field[k];
				  if(k=="id"||k=="aid"){
					  param[k]=v;
				  }else if(k.indexOf("limits")>=0){
					  param.limits = param.limits+"|"+v;
				  }else{
					  if(v!=""){
					  	constraint[k]=v;
					  }
				  }
			  }
			  
			  param.constraint_select = JSON.stringify(constraint);
				  
			  console.log(JSON.stringify(param));
			  console.log(JSON.stringify(constraint));
			  

			    $.post("statement/upLimit",param,function(data){
		    		
			    	if(data.code==0){
			    		layer.msg("保存成功")
			    		layer.closeAll();
			    	}else{
			    		layer.msg(data.msg);
			    	}
			    	$(".layui-laypage-btn").click();
			    },"JSON")
			 
		  });
		  
		  /*  console.log(layero)
		    var formdata = layero.find("form").serializeArray();
			var d = {'limits':'SE','sid':${id}};
			$.each(formdata, function () {
		    	 var v = this.value;
		    	 if(this.name.indexOf("limits")==0){
		    		 d.limits = d.limits+"|"+v;
		    	 }else{
		    	 	if(v!="")d[this.name] = v; 
		    	 }
		     });
			
		    console.log("data:"+JSON.stringify(d))
		    
		    $.post("statement/upLimit",d,function(data){
		    	if(data.code==0){
		    		layer.close(index);
		    		layer.msg("保存成功")
		    		$(".layui-laypage-btn").click();;
		    	}else{
		    		layer.msg(data.msg);
		    	}
		    	
		    },"JSON")  */
		    /* layer.close(index); */ //如果设定了yes回调，需进行手工关闭
	  }
    });
	
}

//状态修改
function upstatus(data){
	var status = data.STATUS
	$.post("statement/upstatus",{"id":data.ID,"status":status==0?1:0},function(data){
		if(data.res==1){
			layer.msg("操作成功");
			$(".layui-laypage-btn").click();;
		}else{
			layer.msg(data.msg)
		}
		
	})
	
}

function del(id){
	layer.confirm('确认删除该记录', {
		btn: ['确定','取消'] //按钮
	}, function(){
		if(id!=null&&id!=""){
			$.post("statement/delLimit",{"id":id},function(data){
				if(data.code==0){
					layer.msg("删除成功");
					$(".layui-laypage-btn").click();;
				}else{
					layer.msg("删除失败");
				}
			},"JSON")
		}else{
			layer.msg("无效的ID");
		}		
	});

}



</script>

</body>

</html>